﻿/**
 * zebron
 * 登录界面
 */

class LoginPage extends UiFramework.UiAbstract
{
    log: HTMLElement;
    onStart(divSelf: HTMLDivElement)
    {
        // 灰色框
        let grayFrame = document.createElement("div");
        grayFrame.className = "grayFrameShow";
        grayFrame.style.top = "18%";
        grayFrame.style.left = "20%";
        this.divSelf.appendChild(grayFrame);

        // 安装 ID
        let txtTitle = document.createElement("text");
        txtTitle.innerText = "安装ID";
        txtTitle.style.position = "absolute";
        txtTitle.style.top = "20%";
        txtTitle.style.left = "45%";
        txtTitle.style.height = "50%";
        this.divSelf.appendChild(txtTitle);

        // 账号输入框
        let accountFrame = document.createElement("input");
        accountFrame.placeholder = "账号";
        accountFrame.style.position = "absolute";
        accountFrame.style.top = "25%";
        accountFrame.style.width = "54%";
        accountFrame.style.height = "20px";
        accountFrame.style.left = "23%";
        accountFrame.style.background = "#EBEBEB";
        accountFrame.style.borderColor = "#549788";
        accountFrame.className = "inputLine";
        this.divSelf.appendChild(accountFrame);

        // 密码输入框
        let pwdFrame = document.createElement("input");
        pwdFrame.placeholder = "密码";
        pwdFrame.style.position = "absolute";
        pwdFrame.style.top = "30%";
        pwdFrame.style.width = "54%";
        pwdFrame.style.height = "20px";
        pwdFrame.style.left = "23%";
        pwdFrame.style.background = "#EBEBEB";
        pwdFrame.style.borderColor = "#549788";
        pwdFrame.className = "inputLine";
        this.divSelf.appendChild(pwdFrame);

        // 登录按钮
        let btnLogin = document.createElement("input");
        btnLogin.type = "button";
        btnLogin.value = "登录";
        btnLogin.style.top = "40%";
        btnLogin.className = "smallGreenButton";
        this.divSelf.appendChild(btnLogin);

        // 打印日志，测试 json 交互成功
        //this.log = document.createElement("p");
        //this.log.innerText = "aaaaaaaaaaaaaa";
        //this.divSelf.appendChild(this.log);
        // 点击跳转
        btnLogin.onclick = () =>
        {
            alert("恭喜，账号密码正确");
            let login = {} as { url: string, username: string, password: string, msgcode: string };
            login.url = "UserLogin";
            login.username = accountFrame.value;
            login.password = pwdFrame.value;
            login.msgcode = null;

            let tsId = massageMgr.callAndroid(DataFramework.AndriodObject.HTTP, login, this.loginResult);
            massageMgr.call(tsId);
        }
    }

    // 回调方法
    loginResult(json: string) {
        let log = {} as { n: string, v: string };
        log.n = "logIn";
        log.v = "json";
        let tsId = massageMgr.callAndroid(DataFramework.AndriodObject.Log_Show, log, null)
        massageMgr.call(tsId);
        massageMgr.callOff(tsId);

        this.log.innerText = json;
        let resultJson = JSON.parse(json) as { state: boolean, error: string, st: string, fid: string };
        if (resultJson.state) {
            let log = {} as { n: string, v: string };
            log.n = "logIn";
            log.v = "st = " + resultJson.st + "  fid = " + resultJson.fid;
            massageMgr.call(massageMgr.callAndroid(DataFramework.AndriodObject.Log_Show, log, null));
            alert("恭喜，账号密码正确");
            this.stateMgr.changeStateByName("configureUI");
        } else {
            alert("账号密码不正确，请检查后重新输入");
        }


        
    }

    onMessage(meassge: any)
    {
        
    }

    onEnd()
    {

    }

    onEvent(event: UiFramework.SmartEvent)
    {
        
    }
}

//  界面管理
UiMap["loginPage"] = () =>
{
    return new LoginPage();
}